package study.component;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
import study.entity.Message;

import java.util.Date;
import java.util.Random;
import java.util.UUID;

@Component
@Slf4j
public class KafkaSender {
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;
   // private Gson gson = new GsonBuilder().create();
    Random ra =new Random();
    //发送消息方法
    public void send() {
       // Message message = new Message();
       // message.setId(System.currentTimeMillis());
       // message.setMsg(UUID.randomUUID().toString());
      //  message.setSendTime(new Date());
       //发送消息格式 “时间戳，随机数，字符串标记”
        String tempMessage= String.format("%d,%d,%s", System.currentTimeMillis(), ra.nextInt(), "test"+ra.nextInt(3));
        log.info("+++++++++++++++++++++  message = {}", tempMessage);
        kafkaTemplate.send("temp", tempMessage);
    }
}
